/*
* @Author: Administrator
* @Date:   2018-01-13 11:29:24
* @Last Modified by:   Administrator
* @Last Modified time: 2018-01-27 12:35:08
*/
import { observable, action, computed, toJS } from 'mobx'
import {
  login,
  messageCount,
  userCollection,
  postTopic,
  message,
  markOne,
  markAll,
} from '../constant/api'
import { post, get } from '../util/request'

class UserStore {
  constructor(user) {
    this.userInfo = user.userInfo || {}
  }
  @observable userInfo
  @observable userMessageCount
  @observable collection = []
  @observable message=[]
  @observable tabIndex=0
  @action login(accessToken) {
    return new Promise((resolve, reject) => {
      post(login, { accessToken }).then((res) => {
        if (res.status === 200 && res.data.success) {
          this.userInfo = res.data.data
          this.getCollection(this.userInfo.loginName).then(() => {
            resolve(this.userInfo)
          })
        } else {
          reject(res.data)
        }
      }).catch(reject)
    })
  }
  @action getMessageCount() {
    return new Promise((resolve, reject) => {
      const messageCountUrl = `${messageCount}`
      const params = {
        needToken: true,
      }
      get(messageCountUrl, params)
        .then((res) => {
          if (res.status === 200 && res.data.success) {
            this.userMessageCount = res.data.data
            resolve()
          } else {
            reject(res)
          }
        }).catch(reject)
    })
  }
  @action getCollection(loginName) {
    return new Promise((resolve, reject) => {
      const userCollectionUrl = `${userCollection}/${loginName}`
      get(userCollectionUrl, {}).then((res) => {
        if (res.status === 200 && res.data.success) {
          const { data } = res.data
          this.collection = data
          resolve()
        }
      }).catch((e) => {
        reject(e)
      })
    })
  }
  @action postTopic(param) {
    const params = Object.assign({}, param, { needToken: true })
    return new Promise((resolve, reject) => {
      post(postTopic, params).then((res) => {
        if (res.status === 200 && res.data.success) {
          resolve(res.data.topic_id)
        } else {
          reject(res.data.error_msg)
        }
      }).catch(e => reject(e))
    })
  }
  @action getMessage() {
    return new Promise((resolve, reject) => {
      get(message, { needToken: true }).then((res) => {
        if (res.status === 200 && res.data.success) {
          this.message = res.data.data.hasnot_read_messages
          resolve()
        } else {
          reject(res.data.error_msg)
        }
      }).catch(e => reject(e))
    })
  }
  @computed get isLogin() {
    return !!this.userInfo && !!this.userInfo.id
  }
  @action changeTab(index) {
    this.tabIndex = index
  }
  @action signOne(messageId) {
    const params = {
      needToken: true,
    }
    const url = markOne(messageId)
    return new Promise((resolve, reject) => {
      post(url, params).then((res) => {
        if (res.status === 200 && res.data.success) {
          this.getMessage().then(() => resolve()).catch(reject)
        } else {
          reject(res.data.error_msg)
        }
      }).catch(e => reject(e))
    })
  }
  @action signAll() {
    const params = {
      needToken: true,
    }
    const url = markAll
    return new Promise((resolve, reject) => {
      post(url, params).then((res) => {
        if (res.status === 200 && res.data.success) {
          this.message = []
        } else {
          reject(res.data.error_msg)
        }
      }).catch(e => reject(e))
    })
  }
  toJson() {
    return {
      userInfo: toJS(this.userInfo),
      message: toJS(this.message),
      userMessageCount: toJS(this.messageCount),
      collection: toJS(this.collection),
      tabIndex: this.tabIndex,
    }
  }
}

export default UserStore
